{-# OPTIONS -Wall #-}

--------------------------------------------------------------------------------
-- |
-- Module      :  Orca.Csound.SignalIO
-- Copyright   :  (c) Stephen Tetley 2013
-- License     :  BSD3
--
-- Maintainer  :  stephen.tetley@gmail.com
-- Stability   :  unstable
-- Portability :  GHC
--
-- Signal input and output.
-- 
--------------------------------------------------------------------------------

module Orca.Csound.SignalIO
  (

  -- * Signal input
    cs_in


  , cs_ins
  , cs_inq
  , cs_soundin
  , cs_diskin
  , cs_diskin2

  -- * Signal output
  , cs_out
  , cs_outs1
  , cs_outs2
  , cs_outs
  , cs_outq
  , cs_soundout
  , cs_soundouts

  -- * Sound file queries
  , cs_filelen
  , cs_filesr
  , cs_filechnls
  , cs_filepeak


  ) where

import Orca.Csound.Monad
import Orca.Csound.Syntax

--------------------------------------------------------------------------------
-- Signal input

-- | Read mono audio. @in@ (Csound) has a name clash with Haskell 
-- so it is called @inm@ here.
--
-- > inm : () -> a1
--
-- WARNING: use only for orchestras with @nchnls=1@, see Csound 
-- reference. 
--
cs_in :: Orca ASig
cs_in = genOpcode1 A_RATE "in" []




-- | Read stereo audio.
--
-- > ins : () -> (a1,a2)
-- 
cs_ins :: Orca (ASig, ASig)
cs_ins = genOpcode2 A_RATE "ins" []


-- | Read quad audio.
--
-- > inq : () -> (a1,a2,a3,a4)
--
cs_inq :: Orca (ASig, ASig, ASig, ASig)
cs_inq = genOpcode4 A_RATE "inq" []




cs_soundin :: IString -> Orca ASig
cs_soundin ifilcod = 
    genOpcode1 A_RATE "soundin" [ getPExpr ifilcod ]



-- | stereo and quad versions need a naming scheme...
--
cs_diskin :: IString -> KSig -> Orca ASig
cs_diskin ifilcod kpitch = 
    genOpcode1 A_RATE "diskin" [ getPExpr ifilcod, getPExpr kpitch ]


-- There is now a @diskin2@ genOpcode in Csound, this means we 
-- can\'t use numeral naming as an arity family for the stereo
-- and quad versions of @diskin@.

cs_diskin2 :: IString -> KSig -> Orca ASig
cs_diskin2 ifilcod kpitch = 
    genOpcode1 A_RATE "diskin2" [ getPExpr ifilcod, getPExpr kpitch ]


--------------------------------------------------------------------------------
-- Signal output


-- | Write mono audio.
--
-- > out : asig -> ()
--
cs_out :: ASig -> Orca ()
cs_out asig = 
    genOpcode0 "out" [ getPExpr asig ]


-- | Write channel 1 of stereo audio.
--
-- > outs1 : asig -> ()
-- 
cs_outs1 :: ASig -> Orca ()
cs_outs1 asig = 
    genOpcode0 "outs1" [ getPExpr asig ]


-- | Write channel 2 of stereo audio.
--
-- > outs2 : asig -> ()
-- 
cs_outs2 :: ASig -> Orca ()
cs_outs2 asig = 
    genOpcode0 "outs2" [ getPExpr asig ]

-- | Write stereo audio.
--
-- > out : asig1 * asig2 -> ()
--
cs_outs :: ASig -> ASig -> Orca ()
cs_outs asig1 asig2 = 
    genOpcode0 "outs" [ getPExpr asig1, getPExpr asig2 ]




-- | Write quad audio.
--
-- > out : asig1 * asig2 * asig3 * asig4 -> ()
--
cs_outq :: ASig -> ASig -> ASig -> ASig -> Orca ()
cs_outq asig1 asig2 asig3 asig4 = 
    genOpcode0 "outq" [ getPExpr asig1, getPExpr asig2
                      , getPExpr asig3, getPExpr asig4 ]

cs_soundout :: ASig -> IString -> Orca ()
cs_soundout asig1 ifilcod = 
    genOpcode0 "soundout" [ getPExpr asig1, getPExpr ifilcod ]

cs_soundouts :: ASig -> ASig -> IString -> Orca ()
cs_soundouts asigl asigr ifilcod = 
    genOpcode0 "soundout" [ getPExpr asigl, getPExpr asigr
                          , getPExpr ifilcod ]


--------------------------------------------------------------------------------
-- File queries


-- Strictly speaking, these should not return /signals/.

-- | Length in seconds...
--
cs_filelen :: IString -> Orca IFloat
cs_filelen ifilcod = 
    genOpcode1_i "filelen" [ getPExpr ifilcod ]


cs_filesr :: IString -> Orca IInt 
cs_filesr ifilcod = 
   genOpcode1_i "filesr" [ getPExpr ifilcod ]

cs_filechnls :: IString -> Orca IInt 
cs_filechnls ifilcod = 
    genOpcode1_i "filechnls" [ getPExpr ifilcod ]

cs_filepeak :: IString -> Orca IFloat
cs_filepeak ifilcod = 
    genOpcode1_i "filepeak" [ getPExpr ifilcod ]



--------------------------------------------------------------------------------
-- Printing and display

-- Note - can\'t type @print@ with current types as we have 3 
-- different iarg types: OString, OInt and OFloat.



